package top.hcode.hoj.manager.coll;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import top.hcode.hoj.dao.collect.CollSelfIntroDao;
import top.hcode.hoj.pojo.entity.coll.CollSelfIntro;

/**
 * @author orangej
 * @since 2025/1/21
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ItechCollSelfIntroManager {
    private final CollSelfIntroDao selfIntroDao;

    @Transactional
    public CollSelfIntro saveSelfIntro(String uid, String collId, String content) {
        CollSelfIntro selfIntro = selfIntroDao.getByUserIdAndCollId(uid, collId);
        if (selfIntro != null) {
            selfIntro.setIntro(content);
            selfIntroDao.updateById(selfIntro);
            return selfIntro;
        }

        selfIntro = new CollSelfIntro().setUserId(uid).setCollId(collId).setIntro(content);
        selfIntroDao.save(selfIntro);

        return selfIntro;
    }

    public CollSelfIntro getSelfIntro(String uid, String collId) {
        return selfIntroDao.getByUserIdAndCollId(uid, collId);
    }

    @Transactional
    public CollSelfIntro deleteSelfIntro(String uid, String collId) {
        CollSelfIntro selfIntro = selfIntroDao.getByUserIdAndCollId(uid, collId);
        if (selfIntro != null) {
            selfIntroDao.removeById(selfIntro.getId());
        }

        return selfIntro;
    }
}
